package com.cyy.demo.ws;

import com.alibaba.fastjson.JSON;
import com.cyy.demo.entity.User;
import lombok.extern.slf4j.Slf4j;

import javax.jws.WebService;
import java.io.UnsupportedEncodingException;
import java.util.*;

/**
 * //name暴露的服务名称, targetNamespace:命名空间,设置为接口的包名倒写(默认是本类包名倒写). endpointInterface接口地址
 */
@Slf4j
@WebService(name = "test", targetNamespace = "http://ws.demo.cyy.com/", endpointInterface = "com.cyy.demo.ws.AppService")
public class AppServiceImpl implements AppService {

    private static HashMap<Integer ,User> userMap;

    static {
        userMap = new HashMap<>();
        userMap.put(1,new User(1,"admin","123456","admin",1,new Date()));
        userMap.put(2,new User());
        userMap.put(3,new User());
        userMap.put(4,new User());
    }

    @Override
    public String getUser(String id) throws UnsupportedEncodingException {

        log.info("==========================="+id);
        return JSON.toJSONString(userMap.get(Integer.parseInt(id)));
    }

    @Override
    public String getUsers() throws UnsupportedEncodingException {
        List<User> users = new ArrayList<>();
        Set<Map.Entry<Integer, User>> entries = userMap.entrySet();
        Iterator<Map.Entry<Integer, User>> iterator = entries.iterator();
        while (iterator.hasNext()){
            users.add(iterator.next().getValue());
        }
        return  JSON.toJSONString(users);
    }
}
